<?php


/*
 * 	Codificado: 02-Noviembre-2011, 00:57
	Estado: Ajustado_Modelo_Actual(X) Funcionando()
 */


class Personas_Convenio_Model extends CI_Model{

	private $table = 'Personas_Convenio';

	function _required($required, $data)
	{
		foreach($required as $field)
		if(!isset($data[$field])) return false;

		return true;
	}

	 
	function Insertar($option = array()) {
		if ($this->_required(array('IdPersona' ,'IdConvenio','TipoPersona' ), $options))
		return FALSE ;

		$this->db->insert($this->table, $options);
		return $this->db->insert_id();

	}

	function Editar($options = array()) {
		if(!$this->_required(array('IdPersona' ,'IdConvenio'),$options))
		return FALSE;

		unset($options['submit'] );

		if(isset($options['IdPersona']))
		$this->db->set('IdPersona', $options['IdPersona']);

		if(isset($options['IdConvenio']))
		$this->db->set('IdConvenio', $options['IdConvenio']);

		if(isset($options['TipoPersona']))
		$this->db->set('TipoPersona', $options['TipoPersona']);

		$this->db->where('IdPersona', $options['IdPersona']);
		$this->db->where('IdConvenio', $options['IdConvenio']);
		$this->db->update($this->table, $options);

		return $this->db->affected_rows();
	}

	function Eliminar($options = array()) {

		$this->db->where('IdPersona', $options['IdPersona']);
		$this->db->where('IdConvenio', $options['IdConvenio']);
		$this->db->delete($this->table);

		if($data['error'] = $this->db->_error_message());

		return $data;

	}

	function Ver($options = array()) {

		$this->db->where('IdPersona', $options['IdPersona']);
		$this->db->where('IdConvenio', $options['IdConvenio']);

		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function Listar($options = array()) {

		$this->db->select('IdPersona,IdConvenio,TipoPersona' );
		if(isset($options['limit']) && isset($options['offset']))
		$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit']))
		$this->db->limit($options['limit']);
			
		$query = $this->db->get($this->table);
		if(isset($options['count']))
		return $query->num_rows();
			
		return $query->result_array();
	}

	function BuscarPorPersona($options = array()) {
		if ($this->_required(array('IdPersona'), $options)) return FALSE ;

		$this->db->where('IdPersona', $options['IdPersona']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorConvenio($options = array()) {
		if ($this->_required(array('IdConvenio'), $options)) return FALSE ;

		$this->db->where('IdConvenio', $options['IdConvenio']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}


	function BuscarPorTipo($options = array()) {
		if ($this->_required(array('TipoPersona'), $options)) return FALSE ;

		$this->db->where('TipoPersona', $options['TipoPersona']);
		$query = $this->db->get($this->TipoPersona);

		return $query->result_array();
	}

}
